package com.comparable_comparator.arraylist;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

class Employee implements Comparable<Employee>{
	 int id;
	 String name;
	 int age;
	Employee(int id,String name,int age){
		this.id=id;
		this.name=name;
		this.age=age;
	}
	public String toString(){
		return "ID: "+id+"\t"+name+"\t"+age;
	}
	public int compareTo(Employee obj){
		Employee em=(Employee)obj;
		int cid=em.id;
		return (this.id-cid);
	}
	
}
public class EmployeeTest {
public static void main(String[] args) {
	Employee e1=new Employee(103, "shashi", 23);
	Employee e2=new Employee(101, "ravi", 25);
	Employee e5=new Employee(101, "ravi", 25);
	Employee e3=new Employee(102, "avinash", 22);
	Employee e4=new Employee(104, "hareram", 28);
	
	ArrayList<Employee> al=new ArrayList<Employee>();
	al.add(e1);
	al.add(e2);
	al.add(e3);
	al.add(e4);
	al.add(e5);
	Collections.sort(al,new SortByName1());
	for(Employee e:al){
		System.out.println(e);
	}
}
}
class SortByName1 implements Comparator{

	@Override
	public int compare(Object o1, Object o2) {
		
		Employee e1=(Employee)o1;
		Employee e2=(Employee)o2;
		String s1=e1.name;
		String s2=e2.name;
		return s1.compareTo(s2);
	}
	
}
